package com.guozcc.bootweb.platform.config.mybatis;


import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class Result2LowerCaseFactory implements ObjectWrapperFactory {

    @Override
    public boolean hasWrapperFor(Object o) {
        return o instanceof Map;
    }

    @Override
    public ObjectWrapper getWrapperFor(MetaObject metaObject, Object o) {
        return new SqlField2LowerCase(metaObject,(Map) o);
    }

//    @Bean
//    public ConfigurationCustomizer mybatisConfigurationCustomizer(){
//        return new ConfigurationCustomizer(){
//            @Override
//            public void customize(Configuration configuration) {
//                configuration.setObjectWrapperFactory(new SqlField2LowerCaseFactory());
//            }
//        };
//    }

    class SqlField2LowerCase extends MapWrapper {

        public SqlField2LowerCase(MetaObject metaObject, Map<String, Object> map) {
            super(metaObject, map);
        }

        @Override
        public String findProperty(String name, boolean useCamelCaseMapping) {
            if(useCamelCaseMapping){
                return name==null?null:name.toLowerCase();
            }
            return name;
        }
    }
}
